/**
 * @file MainWin.cpp
 * @author SoBigDog (623644973lubin@163.com)
 * @brief 
 * @version 0.1
 * @date 2020-04-10
 * 
 * @copyright Copyright (c) 2020
 * 
 */
#include "MainWin.h"

MainWin::MainWin() : m_p_textBox(NULL), m_p_ListBox(NULL), m_p_stateBox(NULL)
{
	m_win_state = WIN_OK;
	initscr();		/* 初始化ncurses, 创建一个stdscr窗口*/
        win = stdscr;
	raw();			// 
	noecho();		/* 屏幕上不返回任何按键 */
	curs_set(0);

	getmaxyx(win, m_max_y, m_max_x); /* 获取屏幕尺寸 */ 
 
	char * c = seer_logo;
 
	//mvprintw(LINES / 2, (COLS - strlen(c)) / 2, c); 
	if (m_max_x < 80) {
		m_win_state = WIN_LEN_TOO_SMALL;
	}

	if (m_max_y < 25) {
		m_win_state = WIN_HEIGHT_TOO_SMALL;
	}

	std::vector<std::string> item;

	switch (m_win_state) {
	case WIN_OK:
		//mvprintw(0, 0, c);
                mvprintw((m_max_y - 15) / 2, (m_max_x - 78) / 2, c);
                wrefresh(win);
		# if release
                sleep(3);
		# else
		usleep(100000);
		# endif
                wclear(win);

		m_p_ListBox = new ListBox(win, m_max_y - 5, m_max_x, 0, 0);
		m_p_stateBox = new StateBox(win, 4, m_max_x, m_max_y - 5, 0);
		m_p_textBox = new TextBox(win, 1, m_max_x, m_max_y - 1, 0);

		m_p_ListBox->push(item);
		m_p_ListBox->fresh();

		break;
	case WIN_LEN_TOO_SMALL:
		printw(win_lenght_insufficient);
		break;
	case WIN_HEIGHT_TOO_SMALL:
		printw(win_height_insufficient);
		break;
	}
}

MainWin::~MainWin()
{
	if (m_p_ListBox != NULL) {
		delete m_p_ListBox;
		m_p_ListBox = NULL;
	}

	if (m_p_textBox != NULL) {
		delete m_p_textBox;
		m_p_textBox = NULL;		
	}

	if (m_p_textBox != NULL) {
		delete m_p_textBox;
		m_p_textBox = NULL;		
	}
}

void MainWin::frash(void)
{
	std::vector<std::string> item;
	std::ostringstream ostr;
	std::string temp_str;

	item.push_back("0x3fffffff");
	item.push_back("1");
	item.push_back("8");
	item.push_back("RX");
	item.push_back("1111111");

	ostr  << "0x" << std::hex << 20 << " " << "0x" << std::hex << 20  << " " << "0x" << std::hex << 20  << " " 
		<< "0x" << std::hex << 20  << " " << "0x" << std::hex << 20  << " " << "0x" << std::hex << 20 << " " 
		<< "0x" << std::hex << 20  << " " << "0x" << std::hex << 20;
	temp_str = ostr.str();
	item.push_back(temp_str);
	//item.push_back("0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07");
	item.push_back("1000");

	// std::cout << item.size() << std::endl;

	int ret = m_p_ListBox->push(item);
	if (ret == -1 )
		m_p_stateBox->print("add error");
	else
		m_p_stateBox->print("add success");

	getmaxyx(win, m_max_y, m_max_x);
	m_p_ListBox->resize(m_max_y -5, m_max_x);
	m_p_stateBox->resize(4, m_max_x);
	m_p_textBox->resize(1, m_max_x);

	m_p_ListBox->fresh();
	m_p_stateBox->fresh();
	//m_p_textBox->fresh();
}
